package com.moxiao.yfjh.module.activity.common;

import org.apache.commons.lang3.time.DateFormatUtils;

import java.security.SecureRandom;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Random;
import java.util.UUID;

/**
 * 生成流水号统一工具类
 **/
public class NoCommonUtil {

    /**
     * 日期路径 即年/月/日 如20180808
     */
    public static String dateTime() {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyyMMdd");
    }

    /**
     * 根据参数生成随机数
     */
    public static Integer getRandom() {
        Random random = new Random();
        int requiredDigits = 4; // 位数要求
        int randomNumber;
        do {
            randomNumber = random.nextInt(9999);
        } while (String.valueOf(randomNumber).length() != requiredDigits);
//        System.out.println("生成的 " + requiredDigits + " 位随机数是：" + randomNumber);
        return randomNumber;
    }

    /**
     * 根据参数生成随机数
     */
    public static Integer getNumRandom() {
        Random random = new Random();
        int requiredDigits = 5; // 位数要求
        int randomNumber;
        do {
            randomNumber = random.nextInt(99999);
        } while (String.valueOf(randomNumber).length() != requiredDigits);
        return randomNumber;
    }


    /**
     * 时间转换
     *
     * @param str
     * @return
     */
    public static Date getDate(String str) {
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date date = dateFormat.parse(str);
            // 创建 SimpleDateFormat 对象来格式化日期
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); // 定义日期字符串的格式
            // 将 Date 对象转换为字符串
            String dateString = dateFormat.format(date);
            String dateStr = dateString + " 23:59:59";
            SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date2 = dateFormat2.parse(dateStr);
            return date2;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 根据特殊标识生成对应的uuid
     *
     * @param str
     * @return
     */
    public static UUID getUuid(String str) {
        // 生成随机的UUID
        UUID uuid = UUID.nameUUIDFromBytes(str.getBytes());
//        // 根据指定的数据生成UUID
//        UUID uuid2 = UUID.nameUUIDFromBytes(str.getBytes());
//        System.out.println("UUID from specified name: " + uuid2);
//        System.out.println("UUID from specified name: " + uuid.toString());
        return uuid;
    }

    private static final String CHARACTERS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

    public static String generateRandomString(int length) {
        SecureRandom random = new SecureRandom();
        StringBuilder sb = new StringBuilder(length);

        for (int i = 0; i < length; i++) {
            int randomIndex = random.nextInt(CHARACTERS.length());
            sb.append(CHARACTERS.charAt(randomIndex));
        }

        return sb.toString();
    }



    /**
     * 根据时间和当前系统时间比较前后
     */
    public static Integer getCompareTo(String dateTime) {
        Integer t = -1;
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
        LocalDateTime date = LocalDateTime.parse(dateTime, formatter);
        // 获取当前系统时间的 Date 对象
        LocalDateTime currentSystemTime = LocalDateTime.now();

        if (date.isBefore(currentSystemTime)) {
            t = 1;
        } else if (date.isAfter(currentSystemTime)) {
            t = 2;
        } else {
            t = 0;
        }
        return t;

    }


    /**
     * 时间转换：将字符串的 1711728000000 转 2024-03-22 09:48:26
     *
     * @param str
     * @return
     */
    public static String getToDate(String str) {
        long timestamp = Long.parseLong(str);
        Date date = new Date(timestamp);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }

    /**
     * 判断字符串是否是日期格式
     *
     * @param input
     * @return
     */

    public static boolean isDateTime(String input) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        sdf.setLenient(false); // 设置严格解析模式，避免宽松解析导致不准确的结果
        try {
            sdf.parse(input);
            return true;
        } catch (ParseException e) {
            return false;
        }
    }


    public static void main(String[] args) {
        System.out.println(generateRandomString(4) + getRandom());
    }

}
